package by.dvrudenko.avtobaza.services.impl;

import java.util.List;

import javax.annotation.PostConstruct;
import javax.inject.Inject;
import javax.persistence.metamodel.SingularAttribute;

import org.apache.commons.lang3.Validate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import by.dvrudenko.avtobaza.dataaccess.ResponsesDao;
import by.dvrudenko.avtobaza.datamodel.Responses;
import by.dvrudenko.avtobaza.services.ResponsesService;

@Service
public class ResponsesServiceImpl implements ResponsesService {
	private static final Logger LOGGER = LoggerFactory.getLogger(ResponsesServiceImpl.class);

	@Inject
	private ResponsesDao dao;
	

	@PostConstruct
	private void init() {
		// Код который будет выполнен после созданиея бина
		// this method will be called by Spring after bean instantiation. Can be
		// used for any initialization process.
		LOGGER.info("Instance of Responses is created. Class is: {}",
				getClass().getName());	
	}

	@Override
	public Responses get(Long id) {
		Responses entity = dao.getById(id);
		return entity;
	}

	@Override
	public void saveOrUpdate(Responses responses) {
		if (responses.getId() == null) {
			LOGGER.debug("Save new: {Responses}", responses);
			dao.insert(responses);
		} else {
			LOGGER.debug("Update: {Responses}", responses);
			dao.update(responses);
		}
	}

	@Override
	public void delete(Responses responses) {
		LOGGER.debug("Remove: {Responses}", responses);
		dao.delete(responses.getId());

	}

	@Override
	public void deleteAll() {
		LOGGER.debug("Remove all Responses");
		dao.deleteAll();
	}

	@Override
	public List<Responses> getRandomResponses(Integer count) {
		if (count<=0){
			throw new IllegalArgumentException("coun must be great then 0, curent value is " + count);
		}
		return dao.getRandomResponses(count);
	}

	@Override
	public Long getCount() {
		return dao.getCount();
	}

	@Override
	public List<Responses> getAllResponses(
			SingularAttribute<Responses, ?> attr, boolean ascending,
			int startRecord, int pageSize) {
		return dao.getAllResponses(attr, ascending, startRecord, pageSize);
	}

}
